<?php

namespace app\home\controller;

use app\home\model\Idfa724Model;
use app\home\model\Order;
use app\home\service\NotifyService;
use app\home\service\OrderService;
use think\Db;
use think\helper\hash\Md5;
use think\Model;
use think\Request;
use think\response\Json;

class Pay800011 extends BasePay{

    const  KEY = '2:SoRKhjSMX_4l5DyMQIUfA8_tm0fgQLb2Jo81LdLcWT3HwLUgYPdFcnOYOhW7AnSi68R1V7ZVyqKgTqnR_5wyfg==:US_2FvDntTImmAZa7PuKIg==';
    const STR = 'com.yindan.game.cafishing';

    public function __construct()
    {
        header('Content-Type: text/html;charset=utf-8');
        header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
        header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
        header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
        header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 
    }
   
    public function log(){
        parent::notify();
        $params = $this->params;
        mylog($params);
    }

    public function order()
    {
        parent::order();
        $data_Arr['code'] = 'success';
        $params = Request::instance()->param();

        $return['orderId'] = $this->order_array['id'];
        $return['money'] = $this->order_array['amount'];
        $return['productName'] = $this->order_array['goods_name'];
        $return['stroeid'] = self::STR.$params['stroeid'];

        exit(json_encode(['code'=>1,'msg'=>'','data'=>$return]));
    }

    


    public function notify()
    {
        

        parent::notify();
        $params = $this->params;
        mylog($params);
        try{
            //验证签名
            if(empty($params['orderId']) || empty($params['UserId']) ||empty($params['ReceiptId'])){
                throw new \Exception('params false');
            }
            $url = "https://appstore-sdk.amazon.com/version/1.0/verifyReceiptId/developer/".self::KEY."/user/".$params['UserId']."/receiptId/".$params['ReceiptId'];
            $res = file_get_contents($url);

            if(empty($res)){
                throw new \Exception('url false'.$res);
            }
            $data = json_decode($res,true);

            if($data['productType'] != 'CONSUMABLE'){
                throw new \Exception('productType false');
            }
            //保存第三方单号
            $tradeLog = (new \app\home\model\TradeLog)->where(['id'=>$params['orderId']])->find();
            if (empty($tradeLog)) {
                throw new \Exception('订单不存在');
            }
            $tradeLog->transactionID =$params['ReceiptId'];
            $tradeLog->save();
            
            if ($tradeLog['goods_id'] !=substr($data['productId'],strlen(self::STR))) {
                throw new \Exception('计费点不一致');
            }
            
            if ($tradeLog["status"] == OrderService::PAY_STATUS_PAID) {
                mylog("800011 订单已经处理过了 订单id".$tradeLog['id']);
                exit('SUCCESS');
            }
            
            //发货
            $notifyService = new NotifyService();
            if($notifyService->notifyProcess($tradeLog) == false){
                throw new \Exception('notifyProcess false');
            }
            exit('SUCCESS');
        }catch (\Exception $exception){
            mylog('800011-error:'.$exception->getMessage().' line:'.$exception->getLine().' file:'.$exception->getFile());
            exit('FAIL');
        }

    }


   
}
